<template>
  <div class="wall">
    <!-- 大标题 -->
    <p class="title">留言墙</p>
    <!-- 标语 -->
    <p class="slogan">开心的或是不开心的，都和树洞🌲说说吧～</p>
    <!-- 标签列表 -->
    <ul class="tags">
      <li class="tag" v-for="(tag, index) in tags" :key="index" :class="{ active: currentChooseTag === index }"
        @click="currentChooseTag = index">{{ tag }}
      </li>
    </ul>
    <!-- 所有的留言 -->
    <div class="notes">
      <card v-for="message in currentMessages" :key="message.message_id" :message="message"
        @openSidebar="handleShowMessage"></card>
    </div>
    <!-- 侧边栏 -->
    <transition name="side-bar">
      <div class="side-bar" v-if="isShowSidebar">
        <div v-if="isCreateMessage">
          <create-message @closeSidebar="handleCloseSidebar"></create-message>
        </div>
        <div v-if="isShowMessage">
          <show-message @closeSidebar="handleCloseSidebar" :message="selectedMessage!"></show-message>
        </div>
      </div>
    </transition>
    <!-- 添加留言按钮 -->
    <div class="add" @click="handleCreateMessage" v-show="!isShowSidebar">
      <span class="iconfont icon-tianjia"></span>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted, computed } from 'vue'
import dayjs from 'dayjs'
import mitt from 'mitt'
import createMessage from './CreateMessage.vue'
import showMessage from './ShowMessage.vue'
import Card from './Card.vue'
import type { MessageType } from '../store/messages'
import { useMessagesStore } from '../store/messages'

// 所有的留言标签
export const tags = ['全部', '留言', '目标', '理想', '过去', '将来', '爱情', '友情', '秘密', '信条', '无题']

// 所有的留言颜色
export const messageColors = [
  'rgba(252, 175, 162, 0.3)',
  'rgba(255, 227, 148, 0.3)',
  'rgba(146, 230, 245, 0.3)',
  'rgba(168, 237, 138, 0.3)',
  'rgba(202, 167, 247, 0.3)',
]

export const emitter = mitt()

export default defineComponent({
  components: { createMessage, showMessage, Card },
  setup() {

    // ****************************************** 控制留言墙主体显示内容 ******************************************

    // 当前显示的留言标签，默认为“全部”
    const currentChooseTag = ref(0)

    emitter.on('toHome', () => {
      currentChooseTag.value = 0
    })

    const messagesStore = useMessagesStore()
    // 根据当前的标签展示不同类别的留言
    let currentMessages = computed(() => {
      if (currentChooseTag.value === 0) {
        return messagesStore.messages
      }
      return messagesStore.messages.filter((message) => message.tag === currentChooseTag.value)
    })

    // 组件挂载时，向服务器发送请求，请求全部留言信息
    onMounted(async () => {
      await messagesStore.fetchMessages()
    })

    // ****************************************** 控制侧边栏显示内容 ******************************************

    const isShowSidebar = ref(false) // 控制显示侧边栏
    const isCreateMessage = ref(false) // 控制侧边栏中显示的内容是否为新建留言
    const isShowMessage = ref(false) // 控制侧边栏中显示的内容是否为留言内容

    // “新建留言”按钮的回调函数
    const handleCreateMessage = () => {
      isShowSidebar.value = true // 首先都必须显示侧边栏
      isShowMessage.value = false // 如果是新建留言，那么展示留言的组件就要隐藏掉
      isCreateMessage.value = true // 设置侧边栏中显示内容为“新建留言”
    }

    let selectedMessage = ref<MessageType>()

    // 点击每一条留言的回调函数
    const handleShowMessage = (message: MessageType) => {
      isShowSidebar.value = true // 首先都必须显示侧边栏
      isCreateMessage.value = false // 如果是展示留言内容，那么新建留言的组件就要隐藏掉
      isShowMessage.value = true // 设置侧边栏中显示内容为“留言内容”
      selectedMessage.value = message // 将选中的留言信息赋值给 selectedMessage
    }

    // 给子组件绑定关闭侧边栏的回调函数
    const handleCloseSidebar = () => {
      isShowSidebar.value = false // 关闭侧边栏
    }

    return { tags, currentChooseTag, currentMessages, messageColors, dayjs, isShowSidebar, isCreateMessage, isShowMessage, selectedMessage, handleCreateMessage, handleShowMessage, handleCloseSidebar }
  }
})
</script>

<style lang="less" scoped>
.wall {
  margin-top: 52px;
  overflow: hidden;

  .title {
    width: 168px;
    height: 78px;
    font-size: 56px;
    font-weight: 700;
    margin: 46px auto 8px;
    background: -webkit-linear-gradient(315deg, #42d392 25%, #647eff);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
  }

  .slogan {
    width: 300px;
    color: #5b5b5b;
    margin: auto;
  }

  .tags {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    margin-top: 44px;

    .tag {
      margin: 0 18px;
      color: #5b5b5b;
      padding: 5px 10px;
      border-radius: 20px;
      box-sizing: border-box;

      &:hover {
        cursor: pointer;
      }

      &.active {
        background-color: #ebebeb;
        font-weight: 600;
        color: #202020;
      }
    }
  }

  .notes {
    display: flex;
    flex-wrap: wrap;
    margin-top: 30px;
    justify-content: center;
  }

  .side-bar {
    width: 360px;
    height: calc(100% - 52px);
    background: rgba(255, 255, 255, 0.80);
    box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.08);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    position: fixed;
    top: 52px;
    right: 0;
    box-sizing: border-box;
    overflow-y: scroll;
  }

  .add {
    width: 56px;
    height: 56px;
    background: #202020;
    box-shadow: 0px 4px 8px 0px rgba(0, 0, 0, 0.08);
    border-radius: 50%;
    position: fixed;
    right: 20px;
    bottom: 20px;
    display: flex;
    justify-content: center;
    align-items: center;

    &:hover {
      cursor: pointer;
    }

    .icon-tianjia {
      font-size: 24px;
      color: #ffffff;
    }
  }
}

.side-bar-enter-active,
.side-bar-leave-active {
  transition: all .3s ease-in;
}

.side-bar-enter-from,
.side-bar-leave-to {
  transform: translateX(100%);
}
</style>